import { isArray, isNumber, isString, orderBy } from "lodash-es";



/**
 * 列表转树形
 * @param list 
 * @param order 
 * @returns 
 */
export function deepTree(list: any[], order?: "asc" | "desc"): any[] {
    const newList: any[] = [];
    const map: any = {};

    list.forEach((e) => (map[e.id] = e));

    list.forEach((e) => {
        const parent = map[e.parentId];

        if (parent) {
            (parent.children || (parent.children = [])).push(e);
        } else {
            newList.push(e);
        }
    });

    const fn = (list: Array<any>) => {
        list.map((e) => {
            if (isArray(e.children)) {
                e.children = orderBy(e.children, "orderNum", order);
                fn(e.children);
            }
        });
    };

    fn(newList);

    return orderBy(newList, "orderNum", order);
}


/**
 * 列表转树形
 * @param list 
 * @param order 
 * @returns 
 */
export function nocopydeepTree(list: any[], order?: "asc" | "desc"): any[] {
    list = JSON.parse(JSON.stringify(list))
    const newList: any[] = [];
    const map: any = {};

    list.forEach((e) => (map[e.id] = e));

    list.forEach((e) => {
        const parent = map[e.parentId];

        if (parent) {
            (parent.children || (parent.children = [])).push(e);
        } else {
            newList.push(e);
        }
    });

    const fn = (list: Array<any>) => {
        list.map((e) => {
            if (isArray(e.children)) {
                e.children = orderBy(e.children, "orderNum", order);
                fn(e.children);
            }
        });
    };

    fn(newList);

    return orderBy(newList, "orderNum", order);
}


/**
 * 列表转树形
 * @param list 
 * @param order 
 * @returns 
 */
export function searchTree(list: any[], name: string): any[] {
    list = JSON.parse(JSON.stringify(list))
    const fn = (list: Array<any>) => {
        return list.filter((e) => {
            if (e.name.includes(name)) {
                return true
            } else {
                if (isArray(e.children)) {
                    e.children = fn(e.children);
                    return !!e.children.length
                } else {
                    return false
                }
            }

        });
    };
    return fn(list);
}

export function getchildrenlast(list: any[]): any[] {

    function fn(list: any[]): any[] {
        const data = list.map((e) => {
            if (isArray(e.children)) {
                return fn(e.children);
            } else {
                return e
            }
        });
        return data
    };
    const data = fn(list)
    return data.flat(Infinity)

}




export function dhms(z: number) {
    let d = 0
    let h = 0
    let m = 0
    let s = 0
    d = Math.floor(z / (1000 * 60 * 60 * 24));
    h = Math.floor((z - d * 1000 * 60 * 60 * 24) / (1000 * 60 * 60));
    m = Math.floor(
        (z - d * 1000 * 60 * 60 * 24 - h * 1000 * 60 * 60) / (1000 * 60)
    );
    s = Math.floor(
        (z -
            d * 1000 * 60 * 60 * 24 -
            h * 1000 * 60 * 60 -
            m * 1000 * 60) /
        1000
    );
    if (z) {
        if (!m) {
            m = 1
        }
    }
    return [
        { name: 'd', t: d },
        { name: 'h', t: h },
        { name: 'm', t: m },
        { name: 's', t: s },
    ];
}


export function geturl(url: string) {
    return url.replace('http:', 'https:')
}


export function getTreeDepth(tree: Tree[]) {
    let maxDepth = 0;

    function traverse(nodes: Tree[], depth: number) {
        if (!nodes || nodes.length === 0) return;
        maxDepth = Math.max(maxDepth, depth);
        for (const node of nodes) {
            if (node.children) {
                traverse(node.children, depth + 1);
            }
        }
    }

    traverse(tree, 1);
    return maxDepth;
}

interface Tree {
    id?: string;
    name?: string;
    children?: Tree[];
    [key: string]: any;
}


/**
 * Recursively search for a leaf node with the given label in the tree structure.
 * @param data The tree structure to search in.
 * @param targetLabel The label to search for.
 * @returns The leaf node with the given label, or null if none is found.
 */
export function findLeafByLabel(data: Tree[], targetLabel: string): Tree | null {
    for (const item of data) {
        // 先检查当前节点
        if (item.name === targetLabel && !item.children) {
            return item; // 找到目标 label 的叶子节点
        }
        // 继续遍历子节点
        if (item.children) {
            const found = findLeafByLabel(item.children, targetLabel);
            if (found) {
                return found; // 在子节点中找到
            }
        }
    }
    return null; // 未找到
}


/**
 *    中间的四位加星
 */
export function getphone(phone?: string) {
    if (isString(phone)) {
        if (phone.length === 11) {
            return phone.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2')
        }
    }
    return phone
}